/// -----------------------------------------------------------------------------------------------------------
/// Module      :  QueueStorageAdapterConnectionFactory.cs
/// Description :  Defines the connection factory for the target system.
/// -----------------------------------------------------------------------------------------------------------

#region Using Directives
using System;
using System.Collections.Generic;
using System.Text;
using System.IdentityModel.Selectors;
using System.ServiceModel.Description;

using Microsoft.ServiceModel.Channels.Common;
#endregion

namespace BizTalkFactory.Adapters.AzureStorage
{
    public class AzureStorageAdapterConnectionFactory : IConnectionFactory
    {
        #region Private Fields

        // Stores the client credentials
        private ClientCredentials clientCredentials;
        // Stores the adapter class
        private AzureStorageAdapter adapter;
        private AzureStorageAdapterConnectionUri uri;

        #endregion Private Fields

        /// <summary>
        /// Initializes a new instance of the QueueStorageAdapterConnectionFactory class
        /// </summary>
        public AzureStorageAdapterConnectionFactory(ConnectionUri connectionUri
            , ClientCredentials clientCredentials
            , AzureStorageAdapter adapter)
        {
            this.clientCredentials = clientCredentials;
            this.adapter = adapter;
            this.uri = connectionUri as AzureStorageAdapterConnectionUri;
        }

        #region Public Properties

        /// <summary>
        /// Gets the adapter
        /// </summary>
        public AzureStorageAdapter Adapter
        {
            get { return this.adapter; }
        }

        /// <summary>
        /// Returns the Connection Uri for this adapter
        /// </summary>
        public AzureStorageAdapterConnectionUri ConnectionUri
        {
            get { return this.uri; }
        }

        #endregion Public Properties

        #region Public Methods

        /// <summary>
        /// Creates the connection to the target system
        /// </summary>
        public IConnection CreateConnection()
        {
            return new AzureStorageAdapterConnection(this);
        }

        /// <summary>
        /// Retrieves the properly formatted connection string to the target system
        /// </summary>
        internal string ConnectionString
        {
            get
            {
                StringBuilder connectionstring = new StringBuilder();

                if (ConnectionUri.UseDevelopmentStorage)
                {
                    // boolean value must be specified in lowercase
                    connectionstring.AppendFormat(";UseDevelopmentStorage={0}", ConnectionUri.UseDevelopmentStorage.ToString().ToLower());

                    if (!String.IsNullOrEmpty(Adapter.DevelopmentStorageProxyUri))
                        connectionstring.AppendFormat(";DevelopmentStorageProxyUri={0}", Adapter.DevelopmentStorageProxyUri);
                }

                else
                {
                    if (!String.IsNullOrEmpty(ConnectionUri.AccountName))
                        connectionstring.AppendFormat(";AccountName={0}", ConnectionUri.AccountName);
                    if (!String.IsNullOrEmpty(Adapter.AccountKey))
                        connectionstring.AppendFormat(";AccountKey={0}", Adapter.AccountKey);

                    if (!String.IsNullOrEmpty(Adapter.BlobEndpoint))
                        connectionstring.AppendFormat(";BlobEndpoint={0}", Adapter.BlobEndpoint);
                    if (!String.IsNullOrEmpty(Adapter.QueueEndpoint))
                        connectionstring.AppendFormat(";QueueEndpoint={0}", Adapter.QueueEndpoint);
                    if (!String.IsNullOrEmpty(Adapter.TableEndpoint))
                        connectionstring.AppendFormat(";TableEndpoint={0}", Adapter.TableEndpoint);

                    if (String.IsNullOrEmpty(Adapter.BlobEndpoint) &&
                        String.IsNullOrEmpty(Adapter.QueueEndpoint) &&
                        String.IsNullOrEmpty(Adapter.TableEndpoint))
                    {
                        if (!String.IsNullOrEmpty(Adapter.SharedAccessSignature))
                            connectionstring.AppendFormat(";SharedAccessSignature={0}", Adapter.SharedAccessSignature);
                        if (!String.IsNullOrEmpty(Adapter.DefaultEndpointsProtocol))
                            connectionstring.AppendFormat(";DefaultEndpointsProtocol={0}", Adapter.DefaultEndpointsProtocol);
                    }
                }

                return connectionstring.Length == 0 ? String.Empty : connectionstring.Remove(0, 1).ToString();
            }
        }

        #endregion Public Methods
    }
}
